package com.baizhi.controller;

import com.baizhi.entity.Catalog;
import com.baizhi.entity.Result;
import com.baizhi.service.CatalogService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("catalog")
public class CatalogController {

    @Autowired
    private CatalogService catalogService;

    @RequestMapping("findAll")
    @ResponseBody
    public List<Catalog> findAll() {
        return catalogService.findAll();
    }

    @RequestMapping("findById")
    @ResponseBody
    public Catalog findById(String id) {
        return catalogService.findById(id);
    }

    @RequestMapping("save")
    @ResponseBody
    public Result save(MultipartFile catalogimage, String name, HttpServletRequest request) {
        Result result = null;
        Catalog catalog = new Catalog();
        try {
            String realPath = request.getSession().getServletContext().getRealPath("/back/image/catalog");
            String uuid = UUID.randomUUID().toString();
            String extension = FilenameUtils.getExtension(catalogimage.getOriginalFilename());
            String urlname = uuid + "." + extension;
            String path = "/back/image/catalog/" + urlname;
            catalog.setId(uuid);
            catalog.setPath(path);
            catalog.setName(name);
            catalogimage.transferTo(new File(realPath, urlname));
            catalogService.save(catalog);
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result();
            result.setMessage("添加失败");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("update")
    public Result update(MultipartFile catalogimage,Catalog catalog,HttpServletRequest request){
        Result result = null;
        try{
            if(!catalogimage.isEmpty()){
                String realPath = request.getSession().getServletContext().getRealPath("/back/image/catalog");
                String extension = FilenameUtils.getExtension(catalogimage.getOriginalFilename());
                String urlname=catalog.getId()+"."+extension;
                String path = "/back/image/catalog/"+urlname;
                catalog.setPath(path);
                catalogimage.transferTo(new File(realPath,urlname));
            }
            catalogService.update(catalog);
        }catch (Exception e){
            e.printStackTrace();
            result = new Result();
            result.setMessage("更新失败");
        }
        return  result;
    }

    @RequestMapping("delete")
    @ResponseBody
    public Result delete(String id,HttpServletRequest request){
        Result result = null;
        try{
            Catalog catalog = catalogService.findById(id);
            String realPath = request.getSession().getServletContext().getRealPath(catalog.getPath());
            File file = new File(realPath);
            file.delete();
            catalogService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result = new Result();
            result.setMessage("删除失败");
        }
        return result;
    }
}
